//
//  PayViewController.m
//  DemoProject
//
//  Created by zhl on 2017/2/9.
//  Copyright © 2017年 zhl. All rights reserved.
//

#import "PayViewController.h"
#import "JfPay.h"
#import "JFTPayModel.h"
#import "MBProgressHUD.h"
#import "HLMBProgressHUD.h"
#import <Foundation/Foundation.h>
#import "JFTDES.h"
#import "JFTDateFormatter.h"
static NSString * const appkey =@"046809144032NqvcGtfF";
static NSString * const aesKey =@"4df3acbf2bcce95d52d0f57c2b7e3570";
static NSString * const aesIv =@"69fc5704171c8f0f";
static NSString * const appId =@"68017104468404";
//static NSString * const appkey =@"0401091344447uuNuuBp";
//static NSString * const aesKey =@"e817a6f244679e1eee1f7050279b8c74";
//static NSString * const aesIv =@"be9e202c46bd33a9";
//static NSString * const appId =@"01017106784401";
@interface PayViewController ()<UITableViewDelegate,UITableViewDataSource,JftSDKPayDelegate>{
}

@property (weak, nonatomic) IBOutlet UITableView *payTableView;
@end

@implementation PayViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _payTableView.delegate =self;
    _payTableView.dataSource =self;
    _payTableView.tableFooterView =[UIView new];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.parArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    NSDictionary *dic =[_parArray objectAtIndex:indexPath.row];
    cell.textLabel.text =[dic objectForKey:@"payTypeName"];
    NSString *productTypeCode =[dic objectForKey:@"productTypeCode"];
    if ([productTypeCode hasPrefix:@"WX"]) {
         cell.imageView.image =[UIImage imageNamed:@"WXSDK"];
    }
    if ([productTypeCode hasPrefix:@"ZF"]) {
        cell.imageView.image =[UIImage imageNamed:@"ZFBSDK"];
    }
    if ([productTypeCode hasPrefix:@"QQ"]) {
        cell.imageView.image =[UIImage imageNamed:@"type11"];
    }
    if ([productTypeCode hasPrefix:@"JD"]) {
        cell.imageView.image =[UIImage imageNamed:@"type12"];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
- (JFTPayModel *)payModel{
    if (!_payModel) {
        _payModel =[JFTPayModel new];
    }
    return _payModel;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
//    typeof(self)weakself =self;
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSDictionary<NSString *,NSString *> * typeItem = [self.parArray objectAtIndex:indexPath.item];
        NSString * typeId = [typeItem objectForKey:@"productTypeCode"];
        NSString *appKey =appkey;
    self.payModel.p1_yingyongnum = appId;
    self.payModel.key            = aesKey;
    self.payModel.iv             = aesIv;
 
    NSDate *p2_orderDate = [NSDate date];
    self.payModel.p2_ordernumber = [JFTDateFormatter getDateStrDate:p2_orderDate dateFormatter:@"yyyyMMddHHmmss" ];
    self.payModel.p3_money       = self.money;
    self.payModel.p6_ordertime   = [JFTDateFormatter getDateStrDate:p2_orderDate dateFormatter:@"yyyyMMddHHmmss" ];
    self.payModel.p7_productcode = typeId;
    NSString *MD5EncryptionString = [JFTDES md532BitUpper:[NSString stringWithFormat:@"%@&%@&%@&%@&%@&%@",self.payModel.p1_yingyongnum,self.payModel.p2_ordernumber,self.payModel.p3_money,self.payModel.p6_ordertime,self.payModel.p7_productcode,appKey]];
        self.payModel.p8_sign = MD5EncryptionString;
        self.payModel.controler =self;
    // parameterDic备选参数，可在文档中查看说明，可以像demo中一样在服务器获取，也可以在程序中写死
           self.payModel.parameterDic =@{@"p10_bank_card_code":@"",@"p11_cardtype":@"",@"p12_channel":@"",@"p13_orderfailertime":@"",@"p14_customname":@"",@"p15_customcontact ":@"",@"p16_customip":@"",@"p17_product":@"",@"p18_productcat":@"",@"p19_productnum":@"",@"p20_pdesc":@"",@"p21_version":@"",@"p23_charset":@"",@"p24_remark":@""};
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [JfPay payByJftPayModel:self.payModel delegate:self];
//        _payModel =nil;
//
//    });
   
}
- (void)dealloc{
        NSLog(@"%s---我释放了",__func__);
    NSLog(@"%@",_payModel);
}
- (void)hudHidden{
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    
}
- (void)openAppSuccessed{
    [self hudHidden];
}
- (void)openAppFailer:(NSString *)failer{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self hudHidden];
        [HLMBProgressHUD addMBProgressHUDinView:self.view hudMode:5 hideDelay:1.2 hudDetailText:failer];
    });

}
- (void)jftPayFailure:(NSString *)message{
    [self hudHidden];
        dispatch_async(dispatch_get_main_queue(), ^{
            [HLMBProgressHUD addMBProgressHUDinView:self.view hudMode:5 hideDelay:1.2 hudDetailText:message];
        });

}
- (void)jftPayResult:(NSString *)result{
    NSLog(@"---result=%@---",result);
//    __weak typeof(self)weakSelf =self;
    dispatch_async(dispatch_get_main_queue(), ^{
//        [weakSelf.navigationController popViewControllerAnimated:YES];
         [HLMBProgressHUD addMBProgressHUDinView:[UIApplication sharedApplication].keyWindow hudMode:5 hideDelay:1.2 hudDetailText:result];
    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
